package edu.mit.media.fv.blocks
{
	import flash.display.*;
	import flash.events.TimerEvent;
	import flash.text.*;
	import flash.utils.Timer;
	
	public class Satellite extends Sprite
	{
		private var _model:Model;
		private var _angle:Number;//angle in radians
		private var _shape:Sprite;
		private var _label:TextField;
		
		private var _timer:Timer;
		private const _partitions:int = 5;
		private const _satRadius:int = 10;
		private var _radius:int = 0;
		
		public function Satellite(angle:Number, model:Model)
		{
			super();
			
			this._angle = angle;
			this._model = model;
			
			this._timer = new Timer(50,_partitions);
			this._timer.addEventListener(TimerEvent.TIMER, onTimer);
			this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			
			this._shape = new Sprite();
			this._shape.graphics.beginFill(0xFFFFFF, 1.0);
			this._shape.graphics.drawCircle(0,0,_satRadius);
			this._shape.graphics.endFill();
			this.addChild(this._shape);
			
			var format:TextFormat = new TextFormat();
			format.align = "left";
			format.bold = true;
			format.font = "Arial";
			format.size = 10;
			this._label = new TextField();
			this._label.width = 100;
			this._label.height = 15;
			this._label.defaultTextFormat = format;
			this._label.type = TextFieldType.DYNAMIC;
			this._label.selectable = false;
			this._label.x = _satRadius;
			this._label.y = 0;
			this._shape.addChild(this._label);
			this._label.visible = false;
		}
		public function get model():Model{
			return this._model;
		}
		
		public function setRadius(radius:int):void{
			this._radius = radius;
			this._label.visible = true;
			this._label.text = this._model.name;
			this._shape.x = this._radius*Math.cos(this._angle);
			this._shape.y = this._radius*Math.sin(this._angle);
		}
		public function show(radius:int = 10):void{
			this._radius = radius;
			this._label.visible = true;
			this._label.text = this._model.name;
			this._timer.reset();
			this._timer.start();
		}
		public function onTimer(event:TimerEvent):void{
			this._shape.x = this._timer.currentCount/this._partitions*this._radius*Math.cos(this._angle);
			this._shape.y = this._timer.currentCount/this._partitions*this._radius*Math.sin(this._angle);
		}
		public function onTimerComplete(event:TimerEvent):void{
			this._shape.x = this._radius*Math.cos(this._angle);
			this._shape.y = this._radius*Math.sin(this._angle);
		}
		public function hide():void{
			this._shape.x = 0;
			this._shape.y = 0;
			this._label.visible = false;
		}
		public function snapBack():void{
			this._shape.x = this._radius*Math.cos(this._angle);
			this._shape.y = this._radius*Math.sin(this._angle);
		}
	}
}